package com.example.wananrdroid.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.wananrdroid.R;
import com.example.wananrdroid.bean.ArticleBean;
import com.example.wananrdroid.bean.BannerBean;
import com.example.wananrdroid.bean.GuangBean;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
    private Context context;
    private List<ArticleBean.DataBean.DatasBean> datas;
    private OnContextClickListener onContextClickListener;
    private List<BannerBean.DataBean> ban;
    private int a=0;
    public interface OnContextClickListener{
        void oncontextclicklistener(View v, int position);
    }

    public void setOnContextClickListener(OnContextClickListener onContextClickListener) {
        this.onContextClickListener = onContextClickListener;
    }

    public HomeAdapter(Context context, List<ArticleBean.DataBean.DatasBean> datas, List<BannerBean.DataBean> ban) {
        this.context = context;
        this.datas = datas;
        this.ban = ban;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==a){
            View inflate = LayoutInflater.from(context).inflate(R.layout.home_banner, parent, false);
            return new Viewholder1(inflate);
        }else{
            View inflate = LayoutInflater.from(context).inflate(R.layout.guang_item, parent, false);
            return new ViewHolder(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (getItemViewType(position)==a){
            Viewholder1 viewholder1= (Viewholder1) holder;
            ArrayList<String> strings = new ArrayList<>();
            for (int i = 0; i < ban.size(); i++) {
                strings.add(ban.get(i).getImagePath());
            }
            viewholder1.home_banner.setImages(strings).setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(context).load(path).into(imageView);
                }
            }).start();
        }else{
            ArticleBean.DataBean.DatasBean datasBean = datas.get(position-1);
            holder.data.setText(datasBean.getNiceDate());
            holder.user.setText(datasBean.getShareUser());
            holder.title.setText(datasBean.getTitle());
            holder.name.setText(datasBean.getChapterName()+"."+datasBean.getSuperChapterName());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onContextClickListener.oncontextclicklistener(view,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size()+1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView user;
        TextView title;
        TextView data;
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            data=itemView.findViewById(R.id.niceShareDate);
            user=itemView.findViewById(R.id.user);
            title=itemView.findViewById(R.id.tilte);
            name=itemView.findViewById(R.id.name);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position==a){
            return a;
        }else{
            return position;
        }
    }

    private class Viewholder1 extends ViewHolder {
        Banner home_banner;
        public Viewholder1(View inflate) {
            super(inflate);
            home_banner=inflate.findViewById(R.id.home_banner);
        }
    }
}
